//
//  NewBankCardViewController.m
//  gpos_app
//
//  Created by 崔航飞 on 2018/8/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "NewBankCardViewController.h"
#import "Masonry.h"
#import "BankName.h"
#import "BankAreaName.h"
#import "WKProgressHUD.h"
#import "SVProgressHUD.h"
#import "GPosAPI.h"
#import "XMRegisterViewController.h"
@interface NewBankCardViewController ()<UIGestureRecognizerDelegate,BankNameDelegate,BankAreaNameDelegate,UITextFieldDelegate>
@property (nonatomic,strong) UIView *backgroundViews;
@property (nonatomic,strong) UIView *view1;
@property (nonatomic,strong) UIView *view2;
@property (nonatomic,strong) UIView *view3;
@property (nonatomic,strong) UIView *view4;
@property (nonatomic,strong) UIView *view5;
@property (nonatomic,strong) UIView *view6;
@property (nonatomic,strong) UIView *view7;
@property (nonatomic,strong) UIImageView *BottomView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITextField *UserTextField;
@property (nonatomic,strong) UITextField *KHZHTextField;
@property (nonatomic,strong) UILabel *view2Lab;
@property (nonatomic,strong) UIView *graryView1;
@property (nonatomic,strong) UIView *graryView2;
@property (nonatomic,strong) UILabel *KHCard;
@property (nonatomic,strong) UILabel *KHArea;
@property (nonatomic,strong) UITextField *textField5;
@property (nonatomic,strong) UITextField *textField6;
@property (nonatomic,strong) UITextField *textField7;
@property (nonatomic,strong)WKProgressHUD * hud;
@property (nonatomic,strong)NSString * banKIdStr;

@end

@implementation NewBankCardViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden=NO;
    //去掉返回的文字
    self.navigationController.navigationBar.topItem.title = @"";
    self.navigationItem.title=@"添加银行卡";
    
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
}
-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    self.navigationController.navigationBar.backgroundColor=[UIColor whiteColor];
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    //利用scrollView设置适配页面
    self.scrollView=[[UIScrollView alloc]init];
    [self.view addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view.mas_top).offset(kNavbarHeight);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.height.equalTo(@(KmainScreenHeiht - kNavbarHeight - kBottomSafeHeight));
        
    }];
    
    self.scrollView.alwaysBounceVertical=YES;
    
    self.scrollView.scrollEnabled=YES;
    
    self.scrollView.showsVerticalScrollIndicator=NO;
    [self initUI];
}
-(void)initUI{
    
    self.backgroundViews=[[UIView alloc] init];
    self.backgroundViews.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    [self.scrollView addSubview:self.backgroundViews];
    [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        
        make.width.equalTo(self.scrollView);
        
    }];
    
    self.view1=[[UIView alloc] init];
    self.view1.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.view1];
    
    [self.view1 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.backgroundViews.mas_top).offset(10);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));

    }];
    
    self.graryView1=[[UIView alloc] init];
    self.graryView1.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    
    [self.backgroundViews addSubview:self.graryView1];
    [self.graryView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.top.equalTo(self.view1.mas_bottom);
        make.height.equalTo(@(42));
        
    }];
    
    self.view2=[[UIView alloc] init];
    self.view2.backgroundColor=[UIColor whiteColor];
    UITapGestureRecognizer *tapView2=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapsView2:)];
    tapView2.delegate=self;
    [self.view2 addGestureRecognizer:tapView2];
    [self.backgroundViews addSubview:self.view2];
    
    [self.view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.graryView1.mas_bottom).offset(0);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    self.graryView2=[[UIView alloc] init];
    self.graryView2.backgroundColor=[UIColor colorWithWhite:0.94 alpha:1];
    
    [self.backgroundViews addSubview:self.graryView2];
    [self.graryView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.top.equalTo(self.view2.mas_bottom);
        make.height.equalTo(@(42));
        
    }];
    
    self.view3=[[UIView alloc] init];
    self.view3.backgroundColor=[UIColor whiteColor];
    UITapGestureRecognizer *tapView3=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapsView3:)];
    tapView3.delegate=self;
    [self.view3 addGestureRecognizer:tapView3];
    [self.backgroundViews addSubview:self.view3];
    
    [self.view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.graryView2.mas_bottom).offset(0);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    self.view4=[[UIView alloc] init];
    self.view4.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.view4];
    
    [self.view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view3.mas_bottom).offset(10);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    self.view5=[[UIView alloc] init];
    self.view5.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.view5];
    
    [self.view5 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view4.mas_bottom).offset(10);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    
    self.view6=[[UIView alloc] init];
    self.view6.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.view6];
    
    [self.view6 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view5.mas_bottom).offset(10);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    self.view7=[[UIView alloc] init];
    self.view7.backgroundColor=[UIColor whiteColor];
    [self.backgroundViews addSubview:self.view7];
    
    [self.view7 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view6.mas_bottom).offset(10);
        make.right.equalTo(self.backgroundViews.mas_right);
        make.left.equalTo(self.backgroundViews.mas_left);
        make.height.equalTo(@(52));
        
    }];
    
    //确定按钮
    self.BottomView =[[UIImageView alloc] init];
    self.BottomView.image=[UIImage imageNamed:@"icon_anniu_f"];
    self.BottomView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(QRBTNS)];
    tap.delegate=self;
    [self.BottomView addGestureRecognizer:tap];
    [self.backgroundViews addSubview:self.BottomView];
    [self.BottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view7.mas_bottom).offset(45);
        make.left.equalTo(self.backgroundViews.mas_left).offset(32);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-32);
        
    }];
    
    UILabel *backLab=[[UILabel alloc] init];
    backLab.text = @"确定";
    backLab.userInteractionEnabled =YES;
    backLab.textColor = [UIColor whiteColor];
    backLab.textAlignment = NSTextAlignmentCenter;
    [self.BottomView addSubview:backLab];
    [backLab mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.BottomView.mas_top).offset(25);
        make.left.equalTo(self.BottomView.mas_left);
        make.right.equalTo(self.BottomView.mas_right);
        make.height.equalTo(@(30));
    }];
    
    self.UserTextField=[[UITextField alloc] init];
    self.UserTextField.placeholder=@"请输入户主姓名";
    // "通过KVC修改占位文字的颜色"
    [self.UserTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.UserTextField.delegate = self;
    [self.view1 addSubview:self.UserTextField];
    [self.UserTextField mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.view1.mas_left).offset(20);
        make.right.equalTo(self.view1.mas_right).offset(-20);
        make.top.equalTo(self.view1.mas_top).offset(13);
        make.bottom.equalTo(self.view1.mas_bottom).offset(-13);
    }];
    
   
    
    UILabel *lab1=[[UILabel alloc] init];
    lab1.text=@"只能添加本人名下银行卡";
    lab1.font=[UIFont systemFontOfSize:14];
    [self.graryView1 addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.backgroundViews.mas_left).offset(20);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-20);
        make.top.equalTo(self.view1.mas_bottom).offset(13);
        make.bottom.equalTo(self.view2.mas_top).offset(-13);

    }];
    
    UILabel *lab2=[[UILabel alloc] init];
    lab2.text=@"开户行所在地";
    lab2.font=[UIFont systemFontOfSize:14];
    [self.graryView2 addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.backgroundViews.mas_left).offset(20);
        make.right.equalTo(self.backgroundViews.mas_right).offset(-20);
        make.top.equalTo(self.view2.mas_bottom).offset(13);
        make.bottom.equalTo(self.view3.mas_top).offset(-13);
        
    }];
    
    self.KHCard=[[UILabel alloc] init];
    self.KHCard.text=@"选择开户行";
    self.KHCard.textColor=[UIColor grayColor];
    self.KHCard.font=[UIFont systemFontOfSize:16];
    [self.view2 addSubview:self.KHCard];
    [self.KHCard mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view2.mas_left).offset(20);
        make.right.equalTo(self.view2.mas_right).offset(-20);
        make.top.equalTo(self.view2.mas_top).offset(13);
        make.bottom.equalTo(self.view2.mas_bottom).offset(-13);
    }];
    
    self.KHArea=[[UILabel alloc] init];
    self.KHArea.text=@"选择开户行所在地";
    self.KHArea.textColor=[UIColor grayColor];
    self.KHArea.font=[UIFont systemFontOfSize:16];
    [self.view3 addSubview:self.KHArea];
    [self.KHArea mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view3.mas_left).offset(20);
        make.right.equalTo(self.view3.mas_right).offset(-50);
        make.top.equalTo(self.view3.mas_top).offset(13);
        make.bottom.equalTo(self.view3.mas_bottom).offset(-13);
    }];
    
    
    self.KHZHTextField=[[UITextField alloc] init];
    self.KHZHTextField.placeholder=@"开户支行名称";
    // "通过KVC修改占位文字的颜色"
    [self.KHZHTextField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.KHZHTextField.delegate = self;
    [self.view4 addSubview:self.KHZHTextField];
    [self.KHZHTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view4.mas_left).offset(20);
        make.right.equalTo(self.view4.mas_right).offset(-20);
        make.top.equalTo(self.view4.mas_top).offset(13);
        make.bottom.equalTo(self.view4.mas_bottom).offset(-13);
    }];
    
    
    self.textField5=[[UITextField alloc] init];
    self.textField5.placeholder=@"卡号,无需空格隔开";
    self.textField5.keyboardType=UIKeyboardTypeNumberPad;
    // "通过KVC修改占位文字的颜色"
    [self.textField5 setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.textField5.delegate = self;
    [self.view5 addSubview:self.textField5];
    [self.textField5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view5.mas_left).offset(20);
        make.right.equalTo(self.view5.mas_right).offset(-20);
        make.top.equalTo(self.view5.mas_top).offset(13);
        make.bottom.equalTo(self.view5.mas_bottom).offset(-13);
    }];
    
    //确认卡号
    self.textField6=[[UITextField alloc] init];
    self.textField6.placeholder=@"确认卡号";
    self.textField6.keyboardType=UIKeyboardTypeNumberPad;
    // "通过KVC修改占位文字的颜色"
    [self.textField6 setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.textField6.delegate = self;
    [self.view6 addSubview:self.textField6];
    [self.textField6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view6.mas_left).offset(20);
        make.right.equalTo(self.view6.mas_right).offset(-20);
        make.top.equalTo(self.view6.mas_top).offset(13);
        make.bottom.equalTo(self.view6.mas_bottom).offset(-13);
    }];
    
    
    //身份证号
    self.textField7=[[UITextField alloc] init];
    self.textField7.placeholder=@"身份证号";
    // "通过KVC修改占位文字的颜色"
    [self.textField7 setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.textField7.delegate = self;
    [self.view7 addSubview:self.textField7];
    [self.textField7 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view7.mas_left).offset(20);
        make.right.equalTo(self.view7.mas_right).offset(-20);
        make.top.equalTo(self.view7.mas_top).offset(13);
        make.bottom.equalTo(self.view7.mas_bottom).offset(-13);
    }];
    
    UIImageView *img2=[[UIImageView alloc] init];
    img2.image=[UIImage imageNamed:@"icon_jt_you"];
    [self.view2 addSubview:img2];
    [img2 mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.view2.mas_top).offset(22);
        make.right.equalTo(self.view2.mas_right).offset(-15);
        
    }];
    
    UIImageView *img3=[[UIImageView alloc] init];
    img3.image=[UIImage imageNamed:@"icon_jt_you"];
    [self.view3 addSubview:img3];
    [img3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view3.mas_top).offset(22);
        make.right.equalTo(self.view3.mas_right).offset(-15);
        
    }];
    
    
    
    
    
    if (self.backgroundViews.subviews.count>0){
        
        [self.backgroundViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.backgroundViews.subviews.lastObject).offset(10);
            
        }];
        
    }
    
}
//选择开户行的点击事件
-(void)tapsView2:(UITapGestureRecognizer *)tp{
    
    BankName *vc=[[BankName alloc] init];
    vc.delegate=self;
    
}
#pragma bankNameDelegate
-(void)backBankName:(NSString *)Name{
    
    self.KHCard.text=Name;
    self.KHCard.textColor=[UIColor blackColor];
    
}
-(void)bankIdName:(NSString *)Name{
    self.banKIdStr = Name;
    NSLog(@"bankId-123--%@",Name);
}
//选择开户行所在地的点击事件
-(void)tapsView3:(UITapGestureRecognizer *)tp{
    
    BankAreaName *vc=[[BankAreaName alloc] init];
    vc.delegate=self;
    
}
#pragma bankAreaDelegate
-(void)backAreaName:(NSString *)Name{
    
    self.KHArea.text=Name;
    self.KHArea.textColor=[UIColor blackColor];
}
-(void)QRBTNS{
    
    if (self.UserTextField.text.length==0) {
      self.hud=[WKProgressHUD popMessage:@"户主姓名不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.KHCard.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"请选择开户行" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.KHArea.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"请选择开户行所在地" inView:self.view duration:1 animated:YES];
        return;
    }
    
    if (self.KHZHTextField.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"开户支行不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.textField5.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"卡号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.textField6.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"确认卡号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.textField7.text.length==0) {
        self.hud=[WKProgressHUD popMessage:@"身份证号不能为空" inView:self.view duration:1 animated:YES];
        return;
    }
    if (self.textField6.text.length!=self.textField5.text.length) {
        self.hud=[WKProgressHUD popMessage:@"两次卡号不一致" inView:self.view duration:1 animated:YES];
        return;
    }
    //requestBindBankCardWithParams
    __weak typeof(self) weakSelf = self;
    int user = [[[NSUserDefaults standardUserDefaults] objectForKey:@"userId"] intValue];
    
        NSDictionary * params = @{@"userId" : @(user),
                                  @"bankId" : self.banKIdStr,
                                  @"userRealName" : self.UserTextField.text,
                                  @"bankLocation" : self.KHArea.text,
                                  @"bankBranchName" : self.KHZHTextField.text,
                                  @"identityCardNumber" : self.textField7.text,
                                  @"bankCardNumber" : self.textField5.text,
                                  @"repeatBankCardNumber" : self.textField6.text
                                  
                                  };
        NSLog(@"pos===%@",params);
        self.BottomView.userInteractionEnabled=NO;
        [GPosAPI requestBindBankCardWithParams:params andCallback:^(id obj) {
            
            if ( obj == nil ){
                weakSelf.BottomView.userInteractionEnabled=YES;
                [SVProgressHUD showErrorWithStatus:[obj objectForKey:@"message"]];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
            }else {
                
                if ([[obj objectForKey:@"code"] intValue] == 200) {
                    [SVProgressHUD showSuccessWithStatus:@"绑定成功"];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                    
                    
                    NSNotification *bankResponse = [NSNotification notificationWithName:@"bangdingyinghangka" object:nil];

                    //                    2.发送通知:
                    [[NSNotificationCenter defaultCenter] postNotification:bankResponse];
                    
                    
                    [self.navigationController popViewControllerAnimated:YES];
                    weakSelf.BottomView.userInteractionEnabled=YES;
                }else if ([[obj objectForKey:@"code"] intValue] == 401 || [[obj objectForKey:@"code"] intValue] == 402) {
                    
                    UIStoryboard *mainStory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"passWord"];
                    //storyboard里的ID
                    XMRegisterViewController *vc = [mainStory instantiateInitialViewController];
                    vc.navigationController.navigationBarHidden=NO;
                    //                    //设置当前窗口的根控制器
                    [UIApplication sharedApplication].keyWindow.rootViewController=vc;
                    weakSelf.BottomView.userInteractionEnabled=YES;
                    [SVProgressHUD showInfoWithStatus:[obj objectForKey:@"message"]];
                    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeFlat];
                    
                }else {
                    weakSelf.BottomView.userInteractionEnabled=YES;
                    weakSelf.hud=[WKProgressHUD popMessage:[obj objectForKey:@"message"] inView:self.view duration:1 animated:YES];
                }
            }
            
            
        }];
    NSLog(@"确认");
    
    
}

#pragma textFieldDelagate(限制输入的电话长度)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.textField5 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField5.text.length >= 30) {
            self.textField5.text = [textField.text substringToIndex:30];
            
            return NO;
        }
    }
    if (textField == self.textField6 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField6.text.length >= 30) {
            self.textField6.text = [textField.text substringToIndex:30];
            
            return NO;
        }
    }
    if (textField == self.textField7 ) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        else if (self.textField7.text.length >= 18) {
            self.textField7.text = [textField.text substringToIndex:18];
            
            return NO;
        }
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
